package com.pfzy.ui.utils;

import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.content.Context;
import android.os.Build;

import java.util.HashMap;

/**
 * 前台服务
 */
public class NotificationChannelUtil {
    private static final String DEFAULT_CHANNEL_ID = "default";
    private static final String DEFAULT_CHANNEL_NAME = "默认";
    //    private static final String SOCKET_DEFAULT_CHANNEL_ID = "socket_channel";
//    private static final String SOCKET_DEFAULT_CHANNEL_NAME = "socket服务";
    private static HashMap<String, NotificationChannel> mChannelList;

//    /**
//     * 默认通知频道ID
//     * @param context
//     * @return
//     */
//    public static String getSocketServiceDefaultNotificationChannelId(Context context) {
//        return getSocketServiceNotificationChannelId(context, SOCKET_DEFAULT_CHANNEL_ID, SOCKET_DEFAULT_CHANNEL_NAME);
//    }
//
//    /**
//     * 通知通道Id
//     * @param context
//     * @param channelId
//     * @param channelName
//     * @return
//     */
//    public static String getSocketServiceNotificationChannelId(Context context, String channelId, String channelName) {
//        initSocketServicNotificationChannel(context, channelId, channelName);
//        return channelId;
//    }
//
//    /**
//     * 初始化通知通道Id
//     * @param context
//     * @param channelId
//     * @param channelName
//     */
//    private static void initSocketServicNotificationChannel(Context context, String channelId, String channelName) {
//        if (context != null && Build.VERSION.SDK_INT >= 26) {
//            if (sChannelList == null) {
//                sChannelList = new HashMap<>();
//            }
//            NotificationChannel channel = sChannelList.get(channelId);
//            if (channel == null) {
//                NotificationChannel channel2 = createNotificationChannel(context.getApplicationContext(), SOCKET_DEFAULT_CHANNEL_ID, SOCKET_DEFAULT_CHANNEL_NAME, 2);
//                sChannelList.put(channelId, channel2);
//            }
//        }
//    }

    /**
     * 获取默认通知频道ID
     */
    public static String getDefaultNotificationChannelId(Context context) {
        return getNotificationChannelId(context, DEFAULT_CHANNEL_ID, DEFAULT_CHANNEL_NAME);
    }

    /**
     * 获取通知频道ID
     */
    public static String getNotificationChannelId(Context context, String channelId, String channelName) {
        initNotificationChannel(context, channelId, channelName);
        return channelId;
    }

    /**
     * 初始化通知频道ID
     */
    private static void initNotificationChannel(Context context, String channelId, String channelName) {
        if (context != null && Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
            if (mChannelList == null) {
                mChannelList = new HashMap<>();
            }
            NotificationChannel channel = mChannelList.get(channelId);
            if (channel == null) {
                NotificationChannel defaultChannel = createNotificationChannel(context.getApplicationContext(), DEFAULT_CHANNEL_ID, DEFAULT_CHANNEL_NAME, 3);
                mChannelList.put(channelId, defaultChannel);
            }
        }
    }

    /**
     * 创建通知频道ID
     * @param context
     * @param channelId
     * @param channelName
     * @param importance
     * @return
     */
    private static NotificationChannel createNotificationChannel(Context context, String channelId, String channelName, int importance) {
        NotificationManager manager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        if (manager == null) {
            return null;
        }
        NotificationChannel channel = null;
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.O) {
            channel = new NotificationChannel(channelId, channelName, importance);
            manager.createNotificationChannel(channel);
        }
        return channel;
    }
}